// JavaScript Document
$(document).ready(function() {
  var module = $("#module").val();

  //Function to hide or show the filter box
  $("#edit-archive").click(function()	{
    //Get the values of the list of checkboxes selected
    var checkbox_values = getSelectedValues();

    //return false if no checkbox is selected
    if (checkbox_values.length == 0) {
      openAlertDialog(300, 125, 'Information', 'Please select atleast one '+module);
      return false;
    }
    makeArchived(checkbox_values);
    return false;
  });
  $('#edit-export').click(function(){
    var pno = $('#hid_pageno').val();
    var data = filterdata_testimonial();
    var sort = $('#hid_sort').val();
    if(sort!=""){
      data+= "&sorting="+sort;
    }
    $('#common-actions').attr('action',baseurl+"testimonial/export?pno="+pno+data);
  });
    
  $('#edit-test-search-date').datepicker({
    dateFormat:'mm-dd-yy'
  });

});

function applyAction (pageno, action) {
  //prepare the field values for filtering
  var search_subject = $.trim($("#edit-test-search-subject").val());
  var search_author = $("#edit-test-search-author").val();
  var search_status = $.trim($("#edit-test-search-status").val());
  var search_date = $.trim($("#edit-test-search-date").val());
  var sort = $('#hid_sort').val();
  //check for search criteria and return false if no value
  if (search_subject == "" && search_author == "" && search_status == "" && search_date == "") {
    openAlertDialog(300, 125, 'Information', 'Please enter the search criteria');
    return;
  }
  var data = "";
  if($.trim(search_subject)!=""){
    if(data!="")
      data+="&"
    data+="search_subject="+search_subject;
  }
  if($.trim(search_author)!=""){
    if(data!="")
      data+="&"
    data+="search_author="+search_author;
  }
  if($.trim(search_status)!="" && search_status != "ANY"){
    if(data!="")
      data+="&"
    data+="search_status="+search_status;
  }
  if($.trim(search_date)!=""){
    if(data!="")
      data+="&"
    data+="search_date="+search_date;
  }
  if($.trim(sort)!=""){
    if(data!="")
      data+="&"
    data+="sorting="+sort;
  }
  $("#listingResultset").html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $("#pagingdivtop").html("");
  $("#pagingdivbottom").html("");
  $.ajax({
    type : "POST",
    url : baseurl+"testimonial/actions/filter"+(action=='archived'?'/archived':''),
    data : data+(pageno>1?'&page_no='+pageno:''),
    cache : false,
    success : function (msg) {
      $("#listingResultset").html(msg);
      var limit = $('#hid_limit').val();
      var page = pageno;
      var counter = $('#hid_totalRec').val();
      if(counter>0){
        var str="";
        var to = page*limit;
        str += '<span>Showing <b>'+(counter>0?(page-1)*limit+1:0)+'</b> to <b>'+(to>counter?counter:to)+'</b> of <b>'+counter+'</b></span>';
        if (page > 1) {
          str += '<a href="javascript:void(0)" onclick = "applyAction('+(page-1)+',\''+action+'\')" class="red"><< Previous</a>';
        }

        if (to < counter) {
          str += ' <span class="red"> |</span><a href="javascript:void(0)" onclick = "applyAction('+(page+1)+',\''+action+'\')" class="red">Next >></a>';
        }

        $("#pagingdivtop").html(str);
        $("#pagingdivbottom").html(str);
      }
    }
  });
}

function makeArchived(checkbox_values){
  $("#pagingdivtop").html("");
  $("#pagingdivbottom").html("");
  var limit = $('#hid_limit').val();
  var counter = $('#hid_totalRec').val();
  $("#listingResultset").html('<div style="padding-top:150px;padding-bottom:150px;text-align:center;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." /></div>');
  $.ajax({
    type : "POST",
    url : baseurl+"testimonial/actions/archive",
    data : "checkbox_values="+checkbox_values,
    cache : false,
    success : function (msg) {
      if (msg == "success") {
        var tot_sel = (checkbox_values+"").split(",");
        reloadListingData(limit,1,"", parseInt(counter, 10)-tot_sel.length,baseurl+"testimonial","page");
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function closeDialog(testId){
  $('#dialog').dialog('destroy');
  makeArchived(testId);
}

function autoReply(){
  openDialog("autoreply",0,780,410,"Manage Auto Reply");
}

function saveautoreply(){
  var fname = $.trim($('#edit-resp-from-name').val());
  var email = $.trim($('#edit-resp-from-email').val());
  var reply = $.trim($('#edit-resp-reply-to').val());
  //var local = $.trim($('#edit-resp-locale').val());
  var local = "EN";
  var sub = $.trim($('#edit-resp-subject').val());
  var resp = editor.getData();
  var str = "";
  if(fname=="")
    str += "From Name should not be empty";
  if(email==""){
    if(str!="")
      str+= "<br/>";
    str += "From Email Address should not be empty";
  }else if(!validateEmail(email)){
    if(str!="")
      str+= "<br/>";
    str += "Email Address not valid";
  }
  if(reply==""){
    if(str!="")
      str+= "<br/>";
    str += "Reply To should not be empty";
  }else if(!validateEmail(reply)){
    if(str!="")
      str+= "<br/>";
    str += "Reply To not valid email address";
  }
  if(local==""){
    if(str!="")
      str+= "<br/>";
    str += "Expired Date should not be empty";
  }
  if(sub==""){
    if(str!="")
      str+= "<br/>";
    str += "Subject should not be empty";
  }
  if($.trim(resp)=="" || $.trim(resp)=="<br />"){
    if(str!="")
      str+= "<br/>";
    str += "Content should not be empty";
  }
  if(str!=""){
    styleStatusMessages(str, "statusMessage", "failure");
    $('#dialog').scrollTop(0);
    return;
  }
  resp = resp.replace(/\n/g,"");
  $('#dialog').html('<div style="padding-top:90px; padding-left:300px;"><img src="'+baseurl+themeurl+'/images/ajax-loader.gif" alt="Loading..." title="Loading... Please Wait !!!" border="0"></div>');
  $.ajax({
    type : "POST",
    url : baseurl+"testimonial/manage_autoreply/save",
    data: 'fname='+fname+"&email="+email+"&reply="+reply+"&local="+local+"&subject="+sub+"&resp="+resp,
    cache : false,
    success : function (msg) {
      if(msg=="Success"){
        $('#dialog').dialog('destroy');
        openAlertDialog(300, 125, 'Information', 'Auto reply message saved successfully');
      }
    },
    error : function (res) {
      handleRequestError(res);
    }
  });
}

function filterdata_testimonial(){
  var search_subject = $.trim($("#edit-test-search-subject").val());
  var search_author = $("#edit-test-search-author").val();
  var search_status = $.trim($("#edit-test-search-status").val());
  var search_date = $.trim($("#edit-test-search-date").val());
  var data = "";
  if($.trim(search_subject)!=""){
    data+="&search_subject="+search_subject;
  }
  if($.trim(search_author)!=""){
    data+="&search_author="+search_author;
  }
  if($.trim(search_status)!="" && search_status != "ANY"){
    data+="&search_status="+search_status;
  }
  if($.trim(search_date)!=""){
    data+="&search_date="+search_date;
  }
  return data;
}

function filterdata_testimonial_archived(){
  return filterdata_testimonial();
}